#include "cylinder.h"
#include "material.h"

#include <GL/glut.h>
#include <cmath>
#include <float.h>

// use qglviewer namespace to avoid having to write qglviewer:: to use
// qglviewer-defined classes
using namespace qglviewer;
using namespace std;

Cylinder::Cylinder()
{
  setBottomRadius(1.0);
  setTopRadius(1.0);
  setHeight(1.0);
}

void Cylinder::draw() const
{
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glMultMatrixd(frame().matrix());
  glColor3fv(material().diffuseColor());
  gluCylinder(gluNewQuadric(), bottomRadius_, topRadius_, height_, 60, 60);
  // Draw discs :
  gluDisk(gluNewQuadric(), 0, bottomRadius_, 60, 60);
  glTranslated(0.0,0.0,height_);
  gluDisk(gluNewQuadric(), 0, topRadius_, 60, 60);
  glTranslated(0.0,0.0,-height_);
  glPopMatrix();
}

/* Does the ray intersect any of the triangles composing
 * the current Cylinder ?
 */
bool Cylinder::intersect(const Ray& ray, Hit& hit) const
{
  float dist = FLT_MAX;

  // to remove "unused parameter" warnings - to be removed
  Q_UNUSED(ray)
  Q_UNUSED(hit)

  // TODO

  return dist != FLT_MAX;
}

/* Parse the XML for elements of the Cylinder
 */
void Cylinder::initFromDOMElement(const QDomElement& e)
{
  // init the object part
  Object::initFromDOMElement(e);
  if (e.hasAttribute("radius")) {
    setRadius(e.attribute("radius").toFloat());
  }
  if (e.hasAttribute("bottomRadius")) {
    setBottomRadius(e.attribute("bottomRadius").toFloat());
  }
  if (e.hasAttribute("topRadius")) {
    setTopRadius(e.attribute("topRadius").toFloat());
  }
  if (e.hasAttribute("height")) {
    setHeight(e.attribute("height").toFloat());
  }
}

